package com.itheima.mp.domain.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author wuJiaWei
 * @version 1.0
 */
@Data
public class PageQuery {

    @ApiModelProperty("页码")
    private Integer pageNo = 1;

    @ApiModelProperty("每页显示条数")
    private Integer pageSize = 10;

    @ApiModelProperty("排序字段")
    private String sortBy;

    @ApiModelProperty("是否升序")
    private Boolean isAsc;

    public <T> Page<T> toMpPage(OrderItem ... items) {
        //1.1 分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        //1.2 排序条件
        if (sortBy != null) {
            page.addOrder(new OrderItem().setColumn(sortBy).setAsc(isAsc));
        }else if (items != null) {
            // 默认按照更新时间排序
            page.addOrder(items);
        }
        return page;
    }
    public <T> Page<T> toMpPage(String defaultSortBy, Boolean defaultAsc) {
        return toMpPage(new OrderItem().setColumn(defaultSortBy).setAsc(defaultAsc));
    }
    public <T> Page<T> toMpPageDefaultSortByCreateTime() {
        return toMpPage(new OrderItem().setColumn("create_time").setAsc(false));
    }
    public <T> Page<T> toMpPageDefaultSortByUpdateTime() {
        return toMpPage(new OrderItem().setColumn("update_time").setAsc(false));
    }

}
